import proj4 from 'proj4';

export function dateFormat (format, date) {
    format = format || 'yyyy-MM-dd';
    const o = {   
        'M+' : date.getMonth() + 1,                 // 月份   
        'd+' : date.getDate(),                    // 日   
        'h+' : date.getHours(),                   // 小时   
        'm+' : date.getMinutes(),                 // 分   
        's+' : date.getSeconds(),                 // 秒   
        'q+' : Math.floor((date.getMonth() + 3) / 3), // 季度   
        'S'  : date.getMilliseconds()             // 毫秒   
    };   
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length));
    }
    for (const k in o) {
        if (new RegExp(`(${k})`).test(format)) {
            format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)));
        } 
    }
    return format;
}

export function alignPostion (align, handler, dropWrap) {
    const rect2 = dropWrap.getBoundingClientRect();
    const rect = handler.getBoundingClientRect();
    const popperStyle = {
        position: 'absolute'
    };
    if (align === 'bottom') {
        popperStyle.top = rect.height;
        popperStyle.left = rect.width / 2 - rect2.width / 2 ;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'bottomLeft') {
        popperStyle.top = rect.height;
        popperStyle.left = 0;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'bottomRight') {
        popperStyle.top = rect.height;
        popperStyle.right = 0;
        popperStyle.willChange = 'top, right';
    }
    if (align === 'top') {
        popperStyle.bottom = rect.height;
        popperStyle.left = rect.width / 2 - rect2.width / 2 ;
    }
    if (align === 'topLeft') {
        popperStyle.bottom = rect.height;
        popperStyle.left = 0;
        popperStyle.willChange = 'bottom, left';
    }
    if (align === 'topRight') {
        popperStyle.bottom = rect.height;
        popperStyle.right = 0;
        popperStyle.willChange = 'bottom, right';
    }
    if (align === 'left') {
        popperStyle.top = rect.height / 2 - rect2.height / 2;
        popperStyle.left = -rect2.width;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'leftTop') {
        popperStyle.top = 0;
        popperStyle.left = -rect2.width;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'leftBottom') {
        popperStyle.bottom = 0;
        popperStyle.left = -rect2.width;
        popperStyle.willChange = 'bottom, left';
    }
    if (align === 'right') {
        popperStyle.top = rect.height / 2 - rect2.height / 2;
        popperStyle.left = rect.width;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'rightTop') {
        popperStyle.top = 0;
        popperStyle.left = rect.width;
        popperStyle.willChange = 'top, left';
    }
    if (align === 'rightBottom') {
        popperStyle.bottom = 0;
        popperStyle.left = rect.width;
        popperStyle.willChange = 'bottom, left';
    }
    return popperStyle;
}


export function transformGeojson (json, src, target) {
    transformFeatures(json.features, src, target);
}

function transformFeatures (features, src, target) {
    features.forEach(feature => {
        const type = feature.geometry.type;
        const coordinates = feature.geometry.coordinates;
        if (type === 'MultiPolygon') {
            feature.geometry.coordinates = transformMultiPolygon(coordinates, src, target);
        }
        if (type === 'Polygon' || type === 'MultiLineString') {
            feature.geometry.coordinates = transformPolygon(coordinates, src, target);
        }
        if (type === 'Point') {
            feature.geometry.coordinates = transformPoint(coordinates, src, target);
        }
        if (type === 'LineString' || type === 'MultiPoint') {
            feature.geometry.coordinates = transformLine(coordinates, src, target);
        }
    });
    return features;
}

/**
 * 多面转换
 * @param {*} polygons 
 */
function transformMultiPolygon (polygons, src, target) {
    const targetCoordinates = [];
    polygons.forEach(polygon => {
        const targetPolygon = transformPolygon(polygon, src, target);
        targetCoordinates.push(targetPolygon);
    });
    return targetCoordinates;
}

/**
 * 面转换
 * @param {*} lines 
 */
function transformPolygon (polygon, src, target) {
    const targetCoordinates = [];
    polygon.forEach(line => {
        const targetLine = transformLine(line, src, target);
        targetCoordinates.push(targetLine);
    });
    return targetCoordinates;
}

/**
 * 线中的点的转换
 * @param {*} points 
 */
function transformLine (points, src, target) {
    const targetPoints = [];
    points.forEach(point => {
        const targetPoint = transformPoint(point, src, target);
        targetPoints.push(targetPoint);
    });
    return targetPoints;
}

/**
 * 点转换
 * @param {*} point 
 */
function transformPoint (point, src, target) {
    return proj4(src, target, point);
}
